package com.ruoyi.tb.lmt.controller;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.tb.lmt.domain.bo.*;
import com.ruoyi.tb.lmt.domain.vo.TbSpecialInspectionVo;
import com.ruoyi.tb.lmt.service.ITbLmtrepairWorkdetailService;
import com.ruoyi.tb.lmt.service.ITbLocomotiveService;
import com.ruoyi.tb.lmt.service.ITbRepairplanlistService;
import com.ruoyi.tb.lmt.service.impl.TbRepairplanlistServiceImpl;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbAcceptanceVo;
import com.ruoyi.tb.lmt.service.ITbAcceptanceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 验收主表
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/acceptance")
public class TbAcceptanceController extends BaseController {

    private final ITbAcceptanceService iTbAcceptanceService;

    @Autowired
    private ITbRepairplanlistService iTbRepairplanlistService;

    @Autowired
    private ITbLmtrepairWorkdetailService iTbLmtrepairWorkdetailService;

    @Autowired
    private ITbLocomotiveService iTbLocomotiveService;

    /**
     * 查询验收主表列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbAcceptanceVo> list(TbAcceptanceBo bo, PageQuery pageQuery) {
        return iTbAcceptanceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出验收主表列表
     */
    @Log(title = "验收主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbAcceptanceBo bo, HttpServletResponse response) {
        List<TbAcceptanceVo> list = iTbAcceptanceService.queryList(bo);
        ExcelUtil.exportExcel(list, "验收主表", TbAcceptanceVo.class, response);
    }

    /**
     * 获取验收主表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xha-lmt:acceptance:query")
    @GetMapping("/{id}")
    public R<TbAcceptanceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTbAcceptanceService.queryById(id));
    }

    /**
     * 新增验收主表
     */
    @SaCheckPermission("xha-lmt:acceptance:add")
    @Log(title = "验收主表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<TbAcceptanceBo> boList) {
        for (TbAcceptanceBo bo : boList) {
            TbAcceptanceBo bos = iTbAcceptanceService.selectByBo(bo);
            iTbAcceptanceService.insertByBo(bos);
        }
        return toAjax(boList.size());
    }

    /**
     * 修改验收主表
     */
    @SaCheckPermission("xha-lmt:acceptance:edit")
    @Log(title = "验收主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbAcceptanceBo bo) {
        return toAjax(iTbAcceptanceService.updateByBo(bo));
    }

    /**
     * 删除验收主表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xha-lmt:acceptance:remove")
    @Log(title = "验收主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbAcceptanceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    //查询验收主表
    @GetMapping("/list/selectByAcceptance")
    public TableDataInfo<TbAcceptanceVo> selectByAcceptance(String id, String lmtType, String lmtNumber, String status, String repairChar, String planName, String workGroup, String workUser, PageQuery pageQuery) {
        return iTbAcceptanceService.selectByAcceptance(id, lmtType, lmtNumber, status, repairChar, planName, workGroup, workUser, pageQuery);
    }

    /**
     * 审批设置验收工队人员
     */
    @SaCheckPermission("xha-lmt:acceptance:updateDispatch")
    @Log(title = "审批验收主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("update/dispatch")
    public R<Void> updateDispatch(@Validated(EditGroup.class) @RequestBody TbAcceptanceBo bo) {
        bo.setStatus("待派活");
        TbRepairplanlistBo rplBo = new TbRepairplanlistBo();
        rplBo.setId(bo.getLmtRepairId());
        rplBo.setStatus("验收中");
        iTbRepairplanlistService.updateByBo(rplBo);
        return toAjax(iTbAcceptanceService.updateByBo(bo));
    }

    /**
     * 设置验收人员
     */
    @SaCheckPermission("xha-lmt:acceptance:updateUser")
    @Log(title = "设置验收人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("update/user")
    public R<Void> updateUser(@Validated(EditGroup.class) @RequestBody TbAcceptanceBo bo) {
        bo.setStatus("验收中");
        bo.setSendToUserTime(new Date());
        return toAjax(iTbAcceptanceService.updateByBo(bo));
    }

    /**
     * 设置验收通过
     */
    @SaCheckPermission("xha-lmt:acceptance:updateApproval")
    @Log(title = "验收通过", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("update/approval")
    public R<Void> updateApproval(@Validated(EditGroup.class) @RequestBody TbAcceptanceBo bo) {
        bo.setStatus("已完成");
        bo.setWorkUser(LoginHelper.getUsername());
        bo.setWorkGroupId(LoginHelper.getDeptId());
        bo.setWorkGroup(LoginHelper.getLoginUser().getDeptName());
        //设置检修中的验收任务完成
        Long lmtRepairId = bo.getLmtRepairId();
        TbLmtrepairWorkdetailBo lmtBo = iTbLmtrepairWorkdetailService.selectByAcceptance(lmtRepairId);
        if (lmtBo != null) {
            //查询验收问题，并且设置完成
            lmtBo.setStatus("已完成");
            lmtBo.setEndTime(new Date());
            iTbLmtrepairWorkdetailService.updateByBo(lmtBo);
        }

        //设置车辆状态为运行中
        Long lmtId = iTbLocomotiveService.selectByLmtId(bo.getLmtRepairId());
        TbLocomotiveBo lmtLocBo = new TbLocomotiveBo();
        lmtLocBo.setID(lmtId);
        lmtLocBo.setStatus("运行中");
        iTbLocomotiveService.updateByBo(lmtLocBo);

        TbRepairplanlistBo rplBo = new TbRepairplanlistBo();
        rplBo.setId(bo.getLmtRepairId());
        rplBo.setStatus("已完成");
        LoginUser currentUser = LoginHelper.getLoginUser();
        rplBo.setCheckUser3(currentUser.getUsername());
        rplBo.setCheckTime3(new Date());
        rplBo.setEndTime(new Date());
        iTbRepairplanlistService.updateByBo(rplBo);
        return toAjax(iTbAcceptanceService.updateByBo(bo));
    }


    @Log(title = "修改验收问题数", businessType = BusinessType.UPDATE)
    @PutMapping("update/workNeed3")
    public R<Void> updateWorkNeed3(@Validated(EditGroup.class) @RequestBody TbAcceptancedetailBo bo) {
        //service层处理传入的bo数据
        TbAcceptanceBo bos = new TbAcceptanceBo();
        bos.setId(bo.getMutualId());
        bos.setWorkNeed3(iTbAcceptanceService.updateWorkNeedByBo(bo.getMutualId()));
        return toAjax(iTbAcceptanceService.updateByBo(bos));
    }
}
